package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.dao.SurchargeDao;
import com.xbongbong.saas.domain.entity.SurchargeEntity;
import com.xbongbong.saas.model.SurchargeModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/3/4 21:31
 * @since v1.0
 */
@Service("surchargeModel")
public class SurchargeModelImpl implements SurchargeModel {
    @Resource
    private SurchargeDao surchargeDao;

    @Override
    public Integer insert(SurchargeEntity entity) throws XbbException {
        return surchargeDao.insert(entity);
    }

    @Override
    public Integer update(SurchargeEntity entity) throws XbbException {
        return surchargeDao.update(entity);
    }

    @Override
    public List<SurchargeEntity> findEntitys(Map<String, Object> param) {
        return surchargeDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return surchargeDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public SurchargeEntity getByKey(Long key, String corpid) {
        return surchargeDao.getByKey(key, corpid);
    }

    @Override
    public JSONArray getAllSurcharge(String corpid, Long formId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("formId", formId);
        param.put("del", 0);
        List<SurchargeEntity> surchargeList = findEntitys(param);

        JSONArray surchargeArray = new JSONArray();
        for (SurchargeEntity surcharge : surchargeList) {
            JSONObject surchargeObj = new JSONObject();
            surchargeObj.put("surchargeName", surcharge.getName());
            surchargeObj.put("surchargeType", surcharge.getType());
            surchargeArray.add(surchargeObj);
        }

        return surchargeArray;
    }

    @Override
    public void initSurcharge(String corpid, Long formId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", 0);
        param.put("name", "优惠");
        param.put("type", 2);
        if (getEntitysCount(param) > 0) {
            return;
        }

        SurchargeEntity surcharge1 = new SurchargeEntity();
        surcharge1.setCorpid(corpid);
        surcharge1.setName("优惠");
        surcharge1.setFormId(formId);
        surcharge1.setType(2);

        SurchargeEntity surcharge2 = new SurchargeEntity();
        surcharge2.setCorpid(corpid);
        surcharge2.setName("其他费用");
        surcharge2.setFormId(formId);
        surcharge2.setType(1);
        List<SurchargeEntity> surchargeList = new ArrayList<>();
        surchargeList.add(surcharge1);
        surchargeList.add(surcharge2);

        surchargeDao.insertBatch(surchargeList);
    }

    @Override
    public List<SurchargeEntity> getAllSurchargeList(String corpid, Long formId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("formId", formId);
        param.put("del", 0);
        return findEntitys(param);
    }

}
